<template>
  <div class="login-container">

    <el-form ref="loginForm" :model="loginForm" class="login-form" autocomplete="on" label-position="left"
             :rules="loginRules">

      <div class="title-container">
        <h3 class="title">海量数据</h3>
        <h3 class="title">管理端</h3>
      </div>

      <el-form-item prop="username">
                <span class="svg-container">
                    <Icon icon="user"/>
                </span>
        <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            @keyup.enter.native="handleLogin"/>
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
                <span class="svg-container">
                    <icon icon="password"/>
                </span>
          <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
                    <icon :icon="passwordType === 'password'?'eye' : 'eye-open'"/>
                </span>
        </el-form-item>
      </el-tooltip>

      <div style="display: flex; justify-content: space-between;margin-bottom:10px">
        <el-form-item prop="code" style="width:70%;">
                    <span class="svg-container">
                        <icon icon="size"/>
                    </span>
          <el-input v-model="loginForm.code" placeholder="验证码" name="code" type="text"/>
        </el-form-item>
        <div @click="getCodeImage()">
          <img :src="codeImage" height="45px" alt="">
        </div>
      </div>

      <div style="display: flex; justify-content: flex-end;margin-bottom:10px">
        <el-form-item prop="code" style="background:transparent;">
          <el-radio-group v-model="loginForm.userType">
            <el-radio label="systemUser">我是管理员</el-radio>
            <!--                        <el-radio label="shopUser">我是服务公司</el-radio>-->
          </el-radio-group>
        </el-form-item>
      </div>


      <el-button size="large" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleLogin">
        登&nbsp;&nbsp;&nbsp;&nbsp;录
      </el-button>
    </el-form>
  </div>
</template>

<script>
import {mapActions} from 'vuex'
import {validUsername} from '@/utils/validate'
import {Message} from 'element-ui'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        userType: 'systemUser',
      },
      codeImage: '',
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}],
        code: [{required: true, trigger: 'blur', message: '请输入验证码',}],
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},

    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },

  created() {
    // 获取code码换取token
    this.getCodeImage();
  },


  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
    //this.handleLogin()
  },
  methods: {

    // 获取图片验证码
    getCodeImage() {
      // 【API请求】----获取验证码图片
      this.$api.getCodeImage({type: 'math'}).then(response => {
        if (response.code == 0) {
          this.codeImage = response.data;
        } else {
          this.$message({message: '验证码获取失败', type: 'error'})
        }
      }).catch((e) => {
        this.$message({message: '验证码获取失败', type: 'error'})
      })

    },

    ...mapActions({
      login: 'user/login'
    }),
    checkCapslock(e) {
      const {key} = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          //【平台验证码登录】
          this.doLogin();
        } else {
        }
      })
    },

    async doLogin() {
      // 执行登录过程
      this.loading = true;
      this.loading = true
      const form = this.loginForm
      this.login({form}).then(res => {
        if (res.code === 0) {
          this.$message({message: '登录成功', type: 'success'})
          this.$router.replace({
            path: this.redirect || '/'
          })
        } else {
          Message(res.msg)
          this.loading = false
        }
      }).catch(() => {
        this.loading = false
      })
    },

    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;


@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}


.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url('./login.png') no-repeat top center;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    //   position: relative;
    //   width: 520px;
    //   max-width: 100%;
    //   padding: 160px 35px 0;
    //   margin: 0 auto;
    //   overflow: hidden;

    width: 520px;
    height: 430px;
    margin: 150px auto;
    background: rgba(225, 225, 225, 0.2);
    border-radius: 10px;
    padding: 20px 35px 0;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: white;
  }


  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;

    i {
      font-size: 20px;
    }
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: white;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 10px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;

    i {
      font-size: 20px;
    }
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
